package com.cskaoyan.service.impl;

import com.cskaoyan.bean.CskaoyanmallKeyword;
import com.cskaoyan.bean.CskaoyanmallKeywordExample;
import com.cskaoyan.bean.CskaoyanmallSearchHistory;
import com.cskaoyan.bean.CskaoyanmallSearchHistoryExample;
import com.cskaoyan.bean.vo.WXSearchHistoryVO;
import com.cskaoyan.bean.vo.WXSearchIndexVO;
import com.cskaoyan.mapper.CskaoyanmallKeywordMapper;
import com.cskaoyan.mapper.CskaoyanmallSearchHistoryMapper;
import com.cskaoyan.service.WXSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WXSearchServiceImpl implements WXSearchService {
    @Autowired
    CskaoyanmallKeywordMapper keywordMapper;
    @Autowired
    CskaoyanmallSearchHistoryMapper searchHistoryMapper;

    @Override
    public WXSearchIndexVO index() {
        WXSearchIndexVO searchIndexVO = new WXSearchIndexVO( );

        CskaoyanmallKeywordExample keywordExample = new CskaoyanmallKeywordExample( );
        keywordExample.createCriteria( ).andDeletedNotEqualTo(true);
        List<CskaoyanmallKeyword> keywordList = keywordMapper.selectByExample(keywordExample);
        searchIndexVO.setHotKeywordList(keywordList);

        searchIndexVO.setDefaultKeyword(keywordList.get(0));

        CskaoyanmallSearchHistoryExample historyExample = new CskaoyanmallSearchHistoryExample( );
        historyExample.createCriteria( ).andDeletedEqualTo(false);
        List<CskaoyanmallSearchHistory> historyList = searchHistoryMapper.selectByExample(historyExample);
        List<WXSearchHistoryVO> historyVOS = new ArrayList<>( );
        for (CskaoyanmallSearchHistory history : historyList
        ) {
            String keyword = history.getKeyword( );
            WXSearchHistoryVO historyVO = new WXSearchHistoryVO( );
            historyVO.setKeyword(keyword);
            historyVOS.add(historyVO);
        }
        searchIndexVO.setHistoryKeywordList(historyVOS);

        return searchIndexVO;
    }

    @Override
    public void clearHistory() {
        searchHistoryMapper.clearHistory( );
    }


}
